﻿namespace SERFLITEMAX20
{
    using SERFLITEMAX20.DataObjects;
    using System;
    using System.Collections.Generic;

    public static class Support
    {
        public static SortedList<int, Area> areaList = GetAreaList();
        public static SortedList<string, FilterObject> filterObjectList = GetFilterObjectList();
        public static SortedList<int, GameObject> gameobjectList = GetGameObjectList();
        public static SortedList<int, Map> mapList = GetMapList();

        internal static SortedList<int, Area> GetAreaList()
        {
            SortedList<int, Area> list = new SortedList<int, Area>();
            using (Areas areas = Areas.Load())
            {
                foreach (Area area in areas.AreaItemsArray)
                {
                    list.Add(area.id, area);
                }
            }
            return list;
        }

        internal static SortedList<string, FilterObject> GetFilterObjectList()
        {
            filterObjectList = new SortedList<string, FilterObject>();
            using (FilterObjects objects = FilterObjects.Load())
            {
                List<FilterObject> list = new List<FilterObject>();
                foreach (FilterObject obj2 in objects.FilterObjectItemsArray)
                {
                    if (!filterObjectList.ContainsKey(obj2.key))
                    {
                        filterObjectList.Add(obj2.key, obj2);
                    }
                    else
                    {
                        list.Add(obj2);
                    }
                }
                foreach (FilterObject obj3 in list)
                {
                    objects.FilterObjectItemsArray.Remove(obj3);
                }
                objects.Save();
            }
            foreach (GameObject obj4 in gameobjectList.Values)
            {
                string key = obj4.entry + "_GameObject";
                if (!filterObjectList.ContainsKey(key))
                {
                    FilterObject obj5 = new FilterObject();
                    obj5.name = obj4.name;
                    obj5.key = key;
                    obj5.type = "GameObject";
                    obj5.entry = obj4.entry;
                    obj5.typeid = ((GameobjectTypes) obj4.typeid).ToString();
                    obj5.filtered = obj4.filtered;
                    obj5.special = obj4.special;
                    obj5.group = ((GameobjectTypes) obj4.typeid).ToString();
                    filterObjectList.Add(key, obj5);
                }
            }
            return filterObjectList;
        }

        internal static SortedList<int, GameObject> GetGameObjectList()
        {
            gameobjectList = new SortedList<int, GameObject>();
            using (GameObjects objects = GameObjects.Load())
            {
                foreach (GameObject obj2 in objects.GameObjectItemsArray)
                {
                    gameobjectList.Add(obj2.entry, obj2);
                }
            }
            return gameobjectList;
        }

        internal static SortedList<int, Map> GetMapList()
        {
            SortedList<int, Map> list = new SortedList<int, Map>();
            using (Maps maps = Maps.Load())
            {
                foreach (Map map in maps.MapItemsArray)
                {
                    list.Add(map.id, map);
                }
            }
            return list;
        }

        public static void RefreshFilterObjectList()
        {
            filterObjectList = GetFilterObjectList();
        }

        public static void SaveFilterObjectList()
        {
            using (FilterObjects objects = new FilterObjects())
            {
                foreach (FilterObject obj2 in filterObjectList.Values)
                {
                    objects.FilterObjectItemsArray.Add(obj2);
                }
                objects.Save();
            }
        }
    }
}

